package com.abel.bigwater.dataserver.model.ewide

import com.abel.bigwater.dataserver.model.ewide.KmingDataType
import io.netty.buffer.ByteBuf
import java.util.*

class DeviceStatus : TermData(KmingDataType.TermDummy, 3) {
    var back: BitSet? = null

    var lowBattery10: Boolean = false
    var fault48511: Boolean = false
    var devMoved17: Boolean = false

    var breakLine20: Boolean = false
    var magnet21: Boolean = false
    var exceedHigh22: Boolean = false
    var exceedLow23: Boolean = false
    var readMutant24: Boolean = false

    var breakLine30: Boolean = false
    var magnet31: Boolean = false
    var exceedHigh32: Boolean = false
    var exceedLow33: Boolean = false
    var readMutant34: Boolean = false

    override fun bufLength() = 3

    override fun parseBuf(buf: ByteBuf): Boolean {
        BitSet.valueOf(ByteArray(3).also { buf.readBytes(it) }).apply {
            back = this

            lowBattery10 = get(0)
            fault48511 = get(1)
            devMoved17 = get(7)

            breakLine20 = get(8)
            magnet21 = get(9)
            exceedHigh22 = get(10)
            exceedLow23 = get(11)
            readMutant24 = get(12)

            breakLine30 = get(16)
            magnet31 = get(17)
            exceedHigh32 = get(18)
            exceedLow33 = get(19)
            readMutant34 = get(20)
        }

        return true
    }

    override fun pack(buf: ByteBuf) {
        BitSet(24).apply {
            set(0, lowBattery10)
            set(1, fault48511)
            set(7, devMoved17)

            set(8, breakLine20)
            set(9, magnet21)
            set(10, exceedHigh22)
            set(11, exceedLow23)
            set(12, readMutant24)

            set(16, breakLine30)
            set(17, magnet31)
            set(18, exceedHigh32)
            set(19, exceedLow33)
            set(20, readMutant34)

            val ba = this.toByteArray()
            buf.writeBytes(ba.copyOf(3))
        }
    }
}